﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UIWindows.GlobalStatics;
using UIWindows.Tools.Helper;
using XingLucifer.Devices;
using XingLucifer.IBase;
using XingLucifer.IBase.Enums;

namespace UIWindows.Services.Handle
{
    [Attributes.TaskClass(XingLucifer.IBase.Enums.TaskTypes.套杯扫码)]
    public class CupScanCode : ServierBase
    {
        public CupScanCode(DeviceConfigStatic deviceConfig, SystemConfigStatic systemConfig,
            UserConfigStatic userConfig, ParameterStatic parameterStatic, TemporaryStatic temporary, HomeStatic homeStatic, MesParameterStatic mesParameterStatic)
            : base(deviceConfig, systemConfig, userConfig, parameterStatic, temporary, homeStatic, mesParameterStatic)
        {

        }

        public override Task Handle(Context.DataContext context)
        {
            if (!ClearSignal(context)) { return Task.CompletedTask; }
            try
            {
                var plc = _deviceConfig.DeviceList.FirstOrDefault(x => x.ServiceName == context.ServiceName && x.TaskType == TaskTypes.扫描信号).Communication as OmronCIP;
                Models.Devices.DeviceClientModel? device = _deviceConfig.DeviceList.FirstOrDefault(x => x.ServiceName == context.ServiceName && x.TaskType == context.TaskType
                && x.DeviceType == context.DeviceType && x.Index == context.DeviceIndex);
                if (device == null)
                {
                    RunLog($"找不到[{context.DeviceIndex}]号设备");
                    return base.Handle(context);
                }
                DateTime time = DateTime.Now;
                var barcode = ScanCode(2, device.Communication, context.DeviceIndex, false);
                if (!string.IsNullOrEmpty(barcode))
                {
                    for (int i = 0; i < 3; i++)
                    {
                        if (plc.WriteSingle(barcode, context.Address_Struct))
                        {
                            RunLog($"-[{context.DeviceIndex}] 条码:[{barcode}] 写入成功!!!");
                            break;
                        }
                        RunLog($"-[{context.DeviceIndex}] 条码:[{barcode}] 写入失败 第[{i}]次");
                    }
                    WriteResult(1, plc, context.Address_Struct, context);
                }
                else
                {
                    $"{context.TaskType} 扫码失败".RunLog(MessageLevelType.警告);
                    WriteResult(2, plc, context.Address_Struct, context);
                }
            }
            catch (Exception ex)
            {
                RunLog($"异常：{ex}");
            }
            return base.Handle(context);
        }

        /// <summary>
        /// 写入结果
        /// </summary>
        /// <param name="result">1 合格  2 扫码失败</param>
        /// <param name="devices">设备链接</param>
        /// <param name="address">地址</param>
        /// <param name="context">上下文</param>
        private void WriteResult(ushort result, OmronCIP plc, string label, Context.DataContext context)
        {
            for (int i = 0; i < 3; i++)
            {
                if (plc.WriteSingle(result, $"{label}.Code"))
                {
                    RunLog($"第[{i}]次 结果 [{result}] 写入成功", MessageLevelType.信息);
                    return;
                }
                else
                {
                    RunLog($"第[{i}]次 结果 [{result}] 写入失败", MessageLevelType.警告);
                }
            }
        }
    }
}
